package jdbcUtil;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class jdbcUtil {

    private static String url;

    private static String user;

    private static String password;

    static{

        try {
            Properties pro=new Properties();
            pro.load(jdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));

            String driverClass=pro.getProperty("driverClass");

            url=pro.getProperty("url");
            user=pro.getProperty("admin");
            password=pro.getProperty("password");


            try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection con=null;

        try {
            con= DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return con;
    }

    public static void close(Connection con, PreparedStatement pstm, ResultSet rs){

        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(pstm!=null){
            try {
                pstm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


}
